#ifndef __LORAPHY_INTERFACE_H__
#define __LORAPHY_INTERFACE_H__

#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

#define LORA_CODINGRATE                             1   // [1: 4/5,]
													    //  2: 4/6																										//  3: 4/7,
														//  4: 4/8]
                                                        
#define LORA_SYMBOL_TIMEOUT                         100 // Symbols



/* values available for the 'tx_mode' parameter */
#define IMMEDIATE       0
#define TIMESTAMPED     1
#define ON_GPS          2
//#define ON_EVENT      3
//#define GPS_DELAYED   4
//#define EVENT_DELAYED 5

#define PIN_RF_INDICATION   4
#define RF_INDICATION_HIGH  1
#define RF_INDICATION_LOW   0


typedef enum
{
    PHY_STATUS_IDEL = 0,
    PHY_STATUS_TX,
    PHY_STATUS_RX,
    PHY_STATUS_TX_TIMEOUT,
    PHY_STATUS_RX_TIMEOUT,
    PHY_STATUS_RX_ERROR,
}LoRaPhy_status_t;

typedef enum
{
    STAT_CRC_OK,
    STAT_CRC_BAD,
    STAT_NO_CRC,
}STAT;


typedef enum
{
    MODULATION_FSK,
    MODULATION_LORA,
}modulation_t;

typedef enum
{
    BANDWIDTH_125KHZ = 0,
    BANDWIDTH_250KHZ,
    BANDWIDTH_500KHZ,
}LoRa_bandwidth_t;

typedef enum
{
    LoRa_SF6 = 6,
    LoRa_SF7 = 7,
    LoRa_SF8 = 8,
    LoRa_SF9 = 9,
    LoRa_SF10 = 10,
    LoRa_SF11 = 11,
    LoRa_SF12 = 12,
}LoRa_SF_t;

typedef enum
{
    LORA_CR_4_5 = 1,
    LORA_CR_4_6,
    LORA_CR_4_7,
    LORA_CR_4_8
}LoRa_CR_t;

typedef struct
{
    float rssi;
    float lsnr;
    uint32_t count_ms;
    uint8_t rf_chain;
    uint8_t if_chain;
    uint8_t status;
}rx_info_t;

typedef struct
{
    uint32_t count_ms;
    bool imme;
    uint8_t tx_mode;
}tx_info_t;

typedef struct
{
    uint32_t    freq_hz;        /*!> center frequency of TX */
    int8_t      rf_power;       /*!> TX power, in dBm */
    uint8_t     modulation;     /*!> modulation to use for the packet */
    uint8_t     bandwidth;      /*!> modulation bandwidth (LoRa only) */
    uint32_t    datarate;       /*!> TX datarate (baudrate for FSK, SF for LoRa) */
    uint8_t     coderate;       /*!> error-correcting code of the packet (LoRa only) */
    bool        invert_pol;     /*!> invert signal polarity, for orthogonal downlinks (LoRa only) */
    uint8_t     f_dev;          /*!> frequency deviation, in kHz (FSK only) */
    uint16_t    preamble;       /*!> set the preamble length, 0 for default */
    bool        crc;         /*!> if true, do not send a CRC in the packet */
    uint16_t    size;           /*!> payload size in bytes */
    uint8_t     payload[256];   /*!> buffer containing the payload */
    uint8_t     SYNC_word;
    rx_info_t   rx_info;
    tx_info_t   tx_info;
}RF_Parameters_t;


// /**
// @struct lgw_pkt_tx_s
// @brief Structure containing the configuration of a packet to send and a pointer to the payload
// */
// typedef struct 
// {
//     uint32_t    freq_hz;        /*!> center frequency of TX */
//     uint8_t     tx_mode;        /*!> select on what event/time the TX is triggered */
//     uint32_t    count_us;       /*!> timestamp or delay in microseconds for TX trigger */
//     uint8_t     rf_chain;       /*!> through which RF chain will the packet be sent */
//     int8_t      rf_power;       /*!> TX power, in dBm */
//     uint8_t     modulation;     /*!> modulation to use for the packet */
//     uint8_t     bandwidth;      /*!> modulation bandwidth (LoRa only) */
//     uint32_t    datarate;       /*!> TX datarate (baudrate for FSK, SF for LoRa) */
//     uint8_t     coderate;       /*!> error-correcting code of the packet (LoRa only) */
//     bool        invert_pol;     /*!> invert signal polarity, for orthogonal downlinks (LoRa only) */
//     uint8_t     f_dev;          /*!> frequency deviation, in kHz (FSK only) */
//     uint16_t    preamble;       /*!> set the preamble length, 0 for default */
//     bool        no_crc;         /*!> if true, do not send a CRC in the packet */
//     bool        no_header;      /*!> if true, enable implicit header mode (LoRa), fixed length (FSK) */
//     uint16_t    size;           /*!> payload size in bytes */
//     uint8_t     payload[256];   /*!> buffer containing the payload */
// }lgw_pkt_tx_t;


extern xQueueHandle radioRxdone_evt_queue;

extern void interface_LoRaPhy_init(RF_Parameters_t *rx_rf_parameter);
extern int interface_LoRaPhy_SendFrame(RF_Parameters_t * tx_parameters);
extern uint8_t interface_LoRaPhy_status(void);

#endif
